package TestCases;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;

public class StartClient3 {
    static Logger log = LoggerUtils.getLogger(StartClient3.class);

    public static void main(String[] args) {

        String filepath = ConfigUtil.getProperty("filepath", ConstantsUtil.CONFIG_COMMON);
        String filename = ConfigUtil.getProperty("filename", ConstantsUtil.CONFIG_COMMON);
        String max_file = ConfigUtil.getProperty("max_file", ConstantsUtil.CONFIG_COMMON);
        String Interval_time = ConfigUtil.getProperty("Interval_time", ConstantsUtil.CONFIG_COMMON);
        String happenTime = ConfigUtil.getProperty("happenTime", ConstantsUtil.CONFIG_COMMON);

        int n_srcIp = Integer.parseInt(ConfigUtil.getProperty("n_srcIp", ConstantsUtil.CONFIG_COMMON));
        int n_destIp = Integer.parseInt(ConfigUtil.getProperty("n_destIp", ConstantsUtil.CONFIG_COMMON));
        int n_visitTool = Integer.parseInt(ConfigUtil.getProperty("n_visitTool", ConstantsUtil.CONFIG_COMMON));
        int n_operType = Integer.parseInt(ConfigUtil.getProperty("n_operType", ConstantsUtil.CONFIG_COMMON));
        int n_dbUser = Integer.parseInt(ConfigUtil.getProperty("n_dbUser", ConstantsUtil.CONFIG_COMMON));
        int n_ruleName = Integer.parseInt(ConfigUtil.getProperty("n_ruleName", ConstantsUtil.CONFIG_COMMON));
        int riskLev_start = Integer.parseInt(ConfigUtil.getProperty("riskLev_start", ConstantsUtil.CONFIG_COMMON));
        int riskLev_end = Integer.parseInt(ConfigUtil.getProperty("riskLev_end", ConstantsUtil.CONFIG_COMMON));
        int max_num = Integer.parseInt(ConfigUtil.getProperty("max_num", ConstantsUtil.CONFIG_COMMON));

        String date = DateUtil.getDateFormat("yyyy_MM_dd_HH_mm_ss_SSSS");

//		JSONObject Json = new JSONObject(true);
//		JSONArray Cases = new JSONArray();
        JSONObject Case = new JSONObject(true);

        BigInteger id = new BigInteger("165657378100470550");
//		BigInteger happenTime = new BigInteger("1656573781");

        if (happenTime.isEmpty()) {
            happenTime = String.valueOf(DateUtil.getTime());
        }

        String srcMac = "00:0D:E9:08:37:D4";
        String systemUser = "";
        String systemHost = "";

        String destMac = "00:0D:E9:08:37:D5";

        String appAccount = "";
        String sessionId = "3788f2f3-482e-46fd-8cf3-8bcaa79408c9";
        String dbType = "3";

        String dbName = "";
        String tableName = "test_table0000000000";
        String fieldName = "所有字段";
        String operSentence = "select * from test_table0000000000";
        int operSentenceLen = 34;
        String sqlBindValue = "";
        int rowNum = 0;
        String sqlExecTime = "1.461000";
        int sqlResponse = 1;
        String returnContent = "";
        int returnContentLen = 0;
        int dealState = 0;
        String protectObjectName = "oracle_17.1.1.100";
        int ruleType = 0;

        String hwId = "4EB0219A85BEC937";
        String relatedInfo = "";
        int approvalStatus = 0;

        // 客户端ip
        String srcIp = "17.1.2.x";
        int srcPort = 37339;

        // 服务器ip
        String destIp = "17.1.1.x";
        int destPort = 1521;

        // 访问工具
        String visitTool = "jdbc.x";
        // 操作类型
        String operType = "select.x";
        // 数据库账户
        String dbUser = "test.x";
        // 规则名
        String ruleName = "sel.x";
        // 风险等级
//		int riskLev = 2;

        String start_time = "";
        String end_time = "";
        try {

            BufferedOutputStream bos = null;
            int num = 1;
            int file_num = 1;
            int Case_Size = 1;

            String filePath = "";
            if (filename.isEmpty()) {
                filePath = filepath + date + "_" + max_file + "/" + happenTime + "_" + max_num + ".fin";
            } else {
                filePath = filepath + filename + "/" + happenTime + "_" + max_num + ".fin";
            }
            log.info(filePath);
            bos = getbos(filePath);
            start_time = DateUtil.getDate();
            log.info("开始时间：" + start_time);
            long start = System.currentTimeMillis();
            for (int for_srcIp = 1; for_srcIp <= n_srcIp; for_srcIp++) {
                for (int for_destIp = 1; for_destIp <= n_destIp; for_destIp++) {
                    for (int for_visitTool = 1; for_visitTool <= n_visitTool; for_visitTool++) {
                        for (int for_operType = 1; for_operType <= n_operType; for_operType++) {
                            for (int for_dbUser = 1; for_dbUser <= n_dbUser; for_dbUser++) {
                                for (int for_ruleName = 1; for_ruleName <= n_ruleName; for_ruleName++) {
                                    for (int for_riskLev = riskLev_start; for_riskLev <= riskLev_end; for_riskLev++) {
                                        Case = new JSONObject(true);
                                        Case.put("id", id);
                                        Case.put("happenTime", Long.valueOf(happenTime.substring(0, happenTime.length() - 3)));
                                        Case.put("srcIp", srcIp.replace("x", String.valueOf(for_srcIp)));
                                        Case.put("srcPort", srcPort);
                                        Case.put("srcMac", srcMac);
                                        Case.put("systemUser", systemUser);
                                        Case.put("systemHost", systemHost);
                                        Case.put("destIp", destIp.replace("x", String.valueOf(for_destIp)));
                                        Case.put("destPort", destPort);
                                        Case.put("destMac", destMac);
                                        Case.put("visitTool", visitTool.replace("x", String.valueOf(for_visitTool)));
                                        Case.put("appAccount", appAccount);
                                        Case.put("sessionId", sessionId);
                                        Case.put("dbType", dbType);
                                        Case.put("dbUser", dbUser.replace(".x", String.valueOf(for_dbUser)));
                                        Case.put("operType", operType.replace("x", String.valueOf(for_operType)));
                                        Case.put("dbName", dbName);
                                        Case.put("tableName", tableName);
                                        Case.put("fieldName", fieldName);
                                        Case.put("operSentence", operSentence);
                                        Case.put("operSentenceLen", operSentenceLen);
                                        Case.put("sqlBindValue", sqlBindValue);
                                        Case.put("rowNum", rowNum);
                                        Case.put("sqlExecTime", sqlExecTime);
                                        Case.put("sqlResponse", sqlResponse);
                                        Case.put("returnContent", returnContent);
                                        Case.put("returnContentLen", returnContentLen);
                                        Case.put("dealState", dealState);
                                        Case.put("protectObjectName", protectObjectName);
                                        Case.put("ruleType", ruleType);
                                        Case.put("ruleName", ruleName.replace("x", String.valueOf(for_ruleName)));
                                        Case.put("riskLev", for_riskLev);
                                        Case.put("hwId", hwId);
                                        Case.put("relatedInfo", relatedInfo);
                                        Case.put("approvalStatus", approvalStatus);
//										Cases.add(Case);

                                        if (file_num > Integer.parseInt(max_file)) {
                                            bos.close();
                                            String date2 = DateUtil.getDateFormat("yyyy_MM_dd_HH_mm_ss_SSSS");
                                            if (filename.isEmpty()) {
                                                filePath = filepath + date2 + "_" + max_file + "/" + happenTime + "_" + max_num + ".fin";
                                            } else {
                                                filePath = filepath + filename + "/" + happenTime + "_" + max_num + ".fin";
                                            }
//											log.info(filePath);
                                            bos = getbos(filePath);
                                            num = 1;
                                            file_num = 1;
                                            date = date2;
                                        }

                                        if (num > max_num) {
                                            bos.close();
//											String date1 = DateUtil.getDateFormat("yyyy_MM_dd_HH_mm_ss");
                                            if (filename.isEmpty()) {
                                                filePath = filepath + date + "_" + max_file + "/" + happenTime + "_" + max_num + ".fin";
                                            } else {
                                                filePath = filepath + filename + "/" + happenTime + "_" + max_num + ".fin";
                                            }
//											log.info(filePath);
                                            bos = getbos(filePath);
                                            num = 1;
//											Cases.clear();
                                        }

                                        bos.write(Case.toString().getBytes());
                                        bos.write("\r\n".getBytes());
//										log.info(Cases.size());
//										log.info(Case_Size);

                                        num++;
                                        file_num++;
                                        Case_Size++;

                                        happenTime = String.valueOf((long) (Double.parseDouble(happenTime)) + (long) (Double.parseDouble(Interval_time)));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            end_time = DateUtil.getDate();
//			Json.put("线程", Thread.currentThread().getName());
//			Json.put("总数：", Cases.size());
//			Json.put("Json：", Cases);
//		System.out.println(Json);

            bos.close();
            long end = System.currentTimeMillis();
            long time = end - start;
            log.info("结束时间：" + end_time);
            log.info("总计耗时：" + formatTime(time));
            log.info("写入成功，总数：" + (Case_Size - 1));

//			ExecuteShellUtil executeShellUtil = new ExecuteShellUtil("172.19.2.35", 22, "vaas", "3edc$RFV");
//			String result = executeShellUtil.executeForResult("ls /opt/");
//			log.info(result);
//			executeShellUtil.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String formatTime(Long ms) {
        Integer ss = 1000;
        Integer mi = ss * 60;
        Integer hh = mi * 60;
        Integer dd = hh * 24;

        Long day = ms / dd;
        Long hour = (ms - day * dd) / hh;
        Long minute = (ms - day * dd - hour * hh) / mi;
        Long second = (ms - day * dd - hour * hh - minute * mi) / ss;
        Long milliSecond = ms - day * dd - hour * hh - minute * mi - second * ss;

        StringBuffer sb = new StringBuffer();
        if (day > 0) {
            sb.append(day + "天");
        }
        if (hour > 0) {
            sb.append(hour + "小时");
        }
        if (minute > 0) {
            sb.append(minute + "分");
        }
        if (second > 0) {
            sb.append(second + "秒");
        }
        if (milliSecond > 0) {
            sb.append(milliSecond + "毫秒");
        }
        return sb.toString();
    }

    public static BufferedOutputStream getbos(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.getParentFile().exists()) { // 如果父目录不存在，创建父目录
            file.getParentFile().mkdirs();
        }
        if (file.exists()) { // 如果已存在,删除旧文件
            file.delete();
        }
        file = new File(filePath);
        file.createNewFile();

        OutputStream out = new FileOutputStream(file);
        BufferedOutputStream bos = new BufferedOutputStream(out);// 设置接受缓存
        return bos;
    }
}
